*******Data analysis of first part: municipality fixed-effects *****************

cd ".../Brazil_candidates_replication/"
use Brazil_municipality.dta, clear

summ votes_white votes_brown votes_black pop_white pop_black pop_brown votes_legenda turnout

pwcorr pop_white pop_black pop_brown

tab state

xtset state

*Table A1
xtreg votes_white pop_white pop_black votes_legenda turnout, fe
est store m1
xtreg votes_brown pop_white pop_black votes_legenda turnout, fe
est store m2
xtreg votes_black pop_white pop_black votes_legenda turnout, fe
est store m3
xtreg votes_white pop_brown pop_black votes_legenda turnout, fe
est store m4
xtreg votes_brown pop_brown pop_black votes_legenda turnout, fe
est store m5
xtreg votes_black pop_brown pop_black votes_legenda turnout, fe
est store m6
esttab m1 m2 m3 m4 m5 m6, se b stats(N r2_w)


*Figure 2: coeff plots for brown candidates (drop DF from graphs because of too small N)

	xtreg votes_brown pop_white pop_black votes_legenda turnout, fe
est store BR

*Estimate the effect
xtreg votes_brown pop_white pop_black votes_legenda turnout, fe
predict yparda, xb
tabstat yparda, statistics(mean)
adjust pop_white=0 pop_black=0

	
	 forv i = 1/6 {
  quietly reg votes_brown pop_white pop_black votes_legenda turnout if state==`i'
  estimate store state_`i'
  }

  forv i = 8/27 {
  quietly reg votes_brown pop_white pop_black votes_legenda turnout if state==`i'
  estimate store state_`i'
  }
  
 coefplot state_1 || state_2 || state_3 || state_4 || state_5 || state_6 || ///
 state_8 || state_9 || state_10 || state_11 || state_12 || state_13 || ///
 state_14 || state_15 || state_16 || state_17 || state_18 || state_19 || ///
 state_20 || state_21 || state_22 || state_23 || state_24 || state_25 || ///
 state_26 || state_27 || BR, drop(_cons votes_legenda turnout) xline(0) ///
    bycoefs byopts(xrescale)
	
	*sorted by size of white population (from lowest to highest)
coefplot state_3 || state_5 || state_14 || state_4 || state_10 || state_22 || ///
 state_17  || state_25 || state_27 || state_1 || state_2 || state_6 || ///
 state_16 || state_15 || state_21 || state_13 || state_8 ||  ///
 state_9 || state_20 || state_11 || state_19 || state_12 || state_26 || ///
 state_18 || state_23 || state_24 || BR, drop(_cons votes_legenda turnout) xline(0) ///
    bycoefs byopts(xrescale)
	

		
*Figure 3: coeff plots for black candidates (drop DF from graphs because of too small N)
	
xtreg votes_black pop_white pop_black votes_legenda turnout, fe
est store BR

*Estimate the effect
xtreg votes_black pop_white pop_black votes_legenda turnout, fe
predict ypreta, xb
tabstat ypreta, statistics(mean)
adjust pop_white=0 pop_black=0

	
		 forv i = 1/6 {
  quietly reg votes_black pop_white pop_black votes_legenda turnout if state==`i'
  estimate store state_`i'
  }

  forv i = 8/27 {
  quietly reg votes_black pop_white pop_black votes_legenda turnout if state==`i'
  estimate store state_`i'
  }
  
 coefplot state_1 || state_2 || state_3 || state_4 || state_5 || state_6 || ///
 state_8 || state_9 || state_10 || state_11 || state_12 || state_13 || ///
 state_14 || state_15 || state_16 || state_17 || state_18 || state_19 || ///
 state_20 || state_21 || state_22 || state_23 || state_24 || state_25 || ///
 state_26 || state_27 || BR, drop(_cons votes_legenda turnout) xline(0) ///
    bycoefs byopts(xrescale)

		*sorted by size of white population (from lowest to highest)
coefplot state_3 || state_5 || state_14 || state_4 || state_10 || state_22 || ///
 state_17  || state_25 || state_27 || state_1 || state_2 || state_6 || ///
 state_16 || state_15 || state_21 || state_13 || state_8 ||  ///
 state_9 || state_20 || state_11 || state_19 || state_12 || state_26 || ///
 state_18 || state_23 || state_24 || BR, drop(_cons votes_legenda turnout) xline(0) ///
    bycoefs byopts(xrescale)
	
	
		
*Not shown: coeff plots for white candidates (drop DF from graphs because of too small N)

*Whole Brazil
xtreg votes_white pop_white pop_black votes_legenda turnout, fe
est store BR

*Estimate the effect
xtreg votes_white pop_white pop_black votes_legenda turnout, fe
predict ybranca, xb
tabstat ybranca, statistics(mean)
adjust pop_white=0 pop_black=0


 forv i = 1/6 {
  quietly reg votes_white pop_white pop_black votes_legenda turnout if state==`i'
  estimate store state_`i'
  }

  forv i = 8/27 {
  quietly reg votes_white pop_white pop_black votes_legenda turnout if state==`i'
  estimate store state_`i'
  }
  
 coefplot state_1 || state_2 || state_3 || state_4 || state_5 || state_6 || ///
 state_8 || state_9 || state_10 || state_11 || state_12 || state_13 || ///
 state_14 || state_15 || state_16 || state_17 || state_18 || state_19 || ///
 state_20 || state_21 || state_22 || state_23 || state_24 || state_25 || ///
 state_26 || state_27 || BR, drop(_cons votes_legenda turnout) xline(0) ///
    bycoefs byopts(xrescale)
	
*sorted by size of white population (from lowest to highest)
coefplot state_3 || state_5 || state_14 || state_4 || state_10 || state_22 || ///
 state_17  || state_25 || state_27 || state_1 || state_2 || state_6 || ///
 state_16 || state_15 || state_21 || state_13 || state_8 ||  ///
 state_9 || state_20 || state_11 || state_19 || state_12 || state_26 || ///
 state_18 || state_23 || state_24 || BR, drop(_cons votes_legenda turnout) xline(0) ///
    bycoefs byopts(xrescale)
	

*End of municipality-level analysis. For candidate-level analysis see the Readme.
